package com.maomao.random;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

public class RondomUtil {
    private static final Random random = new Random();

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        System.out.println(ranSizeList(list,3));
    }


    public static List<Integer> getRandomList(List<Integer> paramList, int count){
        if(paramList.size()<count){
            return paramList;
        }
        Random random=new Random();
        List<Integer> tempList=new ArrayList<Integer>();
        List<Integer> newList=new ArrayList<Integer>();
        int temp=0;
        for(int i=0;i<count;i++){
            temp=random.nextInt(paramList.size());
            if(!tempList.contains(temp)){
                tempList.add(temp);
                newList.add(paramList.get(temp));
            } else{
                i--;
            }
        }
        return newList;
    }

    public static int randomInt(int maxNumber) {
        int randomNumber = random.nextInt(maxNumber);
        return randomNumber;
    }

    public static <T> List<T> ranSizeList(List<T> list, int size) {
        List<T> result = new ArrayList();
        if (size <= 0) {
            return result;
        } else if (list.size() <= size) {
            return list;
        } else {
            while(result.size() != size) {
                T t = list.get(randomInt(list.size()));
                boolean add = true;
                Iterator var5 = result.iterator();

                while(var5.hasNext()) {
                    T temp = (T) var5.next();
                    if (t.equals(temp)) {
                        add = false;
                        break;
                    }
                }

                if (add) {
                    result.add(t);
                }
            }

            return result;
        }
    }
}
